<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <!-- 注意：label决定当前选中的值以及roleIds传的数组值 加插值展示中文-->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSave">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    // 用户的id 用来查询当前用户的角色信息
    employeeId: { type: String, required: true }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 封装函数用于数据回显
    async loadRoles() {
      // 取一个比较大的页码来一次模拟获取所有的数据
      const { data } = await getRoles(1, 100)
      //   console.log(data)
      this.list = data.data.rows

      const info = await getUserDetailById(this.employeeId)
      console.log('getUserDetailById...........', info)
      // 保存这个员工当前的已经有的角色
      this.roleIds = info.data.data.roleIds
    },
    // 取消
    closeDialog() {
      this.$emit('close')
    },
    // 保存分配的角色信息
    async hSave() {
      await assignRoles({ id: this.employeeId, roleIds: this.roleIds })
      this.$emit('close')
    }
  }
}
</script>
